package com.zyark.web.controller;

import com.zyark.domain.ArticleManage;
import com.zyark.model.ServerResponseModel;
import com.zyark.service.ArticleManageService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by ron on 17-8-30.
 */
@Controller
@RequestMapping(value = "article")
public class ArticleController {

    @Resource
    ArticleManageService articleManageService;

    @RequestMapping(value = "list",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponseModel<ArticleManage> getList(@RequestParam(value = "pageNumber",required = false)Integer pageNumber,
                                                      @RequestParam(value = "pageSize",required = false)Integer pageSize){
        if (pageNumber == null){
            pageNumber = 1;
        }
        if (pageSize == null){
            pageSize = 10;
        }
        return articleManageService.selectPageV2(pageNumber,pageSize);
    }

    @RequestMapping(value = "{id}",method = RequestMethod.DELETE)
    @ResponseBody
    public Object deleteArticle(@PathVariable Integer id){
        return articleManageService.delete(new ArticleManage(id));
    }

    @RequestMapping(value = "{id}",method = RequestMethod.POST)
    @ResponseBody
    public Object updateArticle(@RequestBody ArticleManage articleManage){
        return articleManageService.updateSelective(articleManage);
    }

    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public Object saveOrUpdateArticle(@RequestBody ArticleManage articleManage){
        if (articleManage.getPriority() == null){
            return 0;
        }else {
            ArticleManage articleManageForFind = new ArticleManage();
            articleManageForFind.setPriority(articleManage.getPriority());
            List<ArticleManage> DBData = this.articleManageService.selectByEntityWithSimpleResult(articleManageForFind,null,null);
            if (DBData == null || DBData.size() != 0){
                return 0;
            }
        }
        if (articleManage.getId() != null){
            return articleManageService.updateSelective(articleManage);
        }else {
            articleManage.setCreateTime(new Date());
            articleManage.setViewNumber(0);
            return articleManageService.saveSelective(articleManage);
        }
    }

    @RequestMapping(value = "query/{key}",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponseModel<ArticleManage> getQueryList(@PathVariable String key,@RequestParam(value = "pageNumber",required = false)Integer pageNumber,
                                                           @RequestParam(value = "pageSize",required = false)Integer pageSize){
        if (pageNumber == null){
            pageNumber = 1;
        }
        if (pageSize == null){
            pageSize = 10;
        }
        Example example = new Example(ArticleManage.class);
        example.or().andLike("titile","%"+key+"%");
        return articleManageService.selectPageByExampleV2(example,pageNumber,pageSize);
    }
}
